import random, pyautogui, asyncio, pytesseract, math, time
from db import RolesDB


from core.base_controller import BaseController
from core.utils import focus_wow
from controllers.login_role_controller import LoginRoleController
from controllers.common_actions import CommonActions
from controllers.auto_salvage_card.auto_salvage_card_actions import AutoSalvageCardActions

class AutoWayController(BaseController):
  def __init__(self, task_list, ws_client):
    self.task_list = task_list
    self.ws_client = ws_client
    self.cur_role = None
    self.extra_data = {}
    self.wow_rect = None
    
  async def start(self):
    print("start")
    if self.wow_rect is None:
      self.wow_rect = self.get_wow_rect()
    
    target_post = [0.5555, 0.5555, math.pi]
    self.move_to_point(target_post)
    
  def is_degrees_correct(self, point1, point2):
    return abs(math.degrees(point1[2]) - math.degrees(point2[2])) <= 1
  def get_degrees(self, point1, point2):
    return abs(math.degrees(point1[2]) - math.degrees(point2[2]))
  
  def is_pos_correct(self, point1, point2):
    radius = 5
    distance = math.sqrt((point1[0] - point2[0])**2 + (point1[1] - point2[1])**2)
    return distance <= radius
  
  def move_to_point(self, point):
    role_pos = self.get_role_pos()
    if role_pos is None:
      pyautogui.keyDown("a")
      time.sleep(0.2)
      pyautogui.keyUp("a")
      self.move_to_point(point)
      print("role is not found")
      return
    if not self.is_degrees_correct(role_pos, point):
      pyautogui.moveTo(self.wow_rect[0], self.wow_rect[1] + 100, duration=1)
      while True:
      # 参数配置
        pyautogui.mouseDown(button='right')
        pyautogui.move(3, 0, duration=0.001)
        pyautogui.mouseUp(button='right')
        role_pos = self.get_role_pos()
        if role_pos and self.is_degrees_correct(role_pos, point):
          break
    
  def get_role_pos(self):
    if self.wow_rect is None:
      return None
    numbers = None
    try:
      screenshot = pyautogui.screenshot(region=self.wow_rect)
      ocr_result = pytesseract.image_to_string(screenshot)
      numbers = [float(line) / 100000000000 for line in ocr_result.splitlines() if line.strip()]
    except Exception as e:
      pass
    return numbers
  
  def get_wow_rect(self):
    wow_window = focus_wow()
    game_scale = 1625 / 2100
    if wow_window:
      x, y, width, height = wow_window.left, wow_window.top, wow_window.width, wow_window.height
      # 假设已获取窗口位置 (x, y) 和尺寸 (width, height)
      target_x = int(x + 640 * game_scale)  # 窗口左上角向右偏移80像素
      target_y = y + 30    # 窗口左上角向下偏移640像素
      target_width, target_height = 200, 80
      return (target_x, target_y, int(target_width * game_scale), int(target_height * game_scale))
    return None

  async def extra_actions(self, log_prefix):
    common_actions = CommonActions(log_prefix=log_prefix, ws_client=self.ws_client)
    auto_salvage_card_actions = AutoSalvageCardActions(log_prefix=log_prefix, ws_client=self.ws_client)
    await self.log("extra_actions start", log_prefix=log_prefix)

    ## 收邮件
    await common_actions.get_emails()
    await asyncio.sleep(random.randint(4, 5))
    
    ## 拆分
    await auto_salvage_card_actions.start_salvage()
        
    await self.log("extra_actions end", log_prefix=log_prefix)
